<template>
    <rui-page :defines="defines">

        <div style="background-color: #f9fafc;">
            <Alert show-icon style="height: 30px;">
                <div style="margin-top:-2px" >请输入查询条件</div>
            </Alert>
            <rui-query
                ref="searchForm"
                :query="queryData"
                :query-rows="searchFormLabels"
            />
        </div>
        <div style="background-color: #f9fafc;">
            <rui-table
                :defineId="'manualLabel'"
                :fixedLeft="['opt']"
                :showIndex="false"
                :select="false"
                :columns="manualLabelColumns"
                :slots="[{key:'opt',slot:'opt'}]"
                @loadDatas="queryData"
                ref-query="searchForm"
                ref="labelTable"
            >
                <template slot="toolBar">
                <Button @click="add" type="primary" icon="md-add">新增</Button>
                </template>
                <template slot="opt" slot-scope="{ row }">
                    <div  >
                        <Button @click="edit(row)" size="small"  type="primary" icon="ios-create">编辑</Button>
                        <!--<Button type="error" size="small" icon="md-trash" @click="remove(row)">删除</Button>-->
                        <Button @click="ruleConfig(row)" size="small" type="warning">规则配置</Button>
                        <Button @click="failure(row)" v-if="row.status=='takeEffect'" type="error" size="small">失效</Button>
                        <Button @click="takeEffect(row)" v-if="row.status=='failure'" type="success" size="small">生效</Button>

                    </div>
                </template>
            </rui-table>
        </div>
        <Modal
            v-model="labelModalVisible"
            :title="modalTitle"
            :closable="false"
            :width="1000"
            :mask-closable="false"
            @on-ok="labelSubmit"
        >
            <rui-form :form-options="labelFormLabels" :read-only="false"
                      ref="labelForm" style="margin-top:-10px;height: 250px;">
            </rui-form>

            <div slot="footer">
                <Button size="small" type="primary"  @click="labelModalVisible = false" icon="ios-backspace">取消</Button>
                <Button type="primary" size="small" icon="md-bookmark" @click="labelSubmit">保存</Button>
            </div>
        </Modal>
        <Modal
            v-model="isShowRuleConfig"
            :title="ruleConfigTitle"
            :width="1000"
            :closable="false"
            :mask-closable="false">
            <rule-tree :readOnly="ruleReadOnly"
                v-if="dataLoad" :rule-data="ruleData" :ruleAtoms="ruleAtoms" :ruleName="ruleName" :ruleGroup="ruleGroup" :businessScenario="businessScenario" @saved="returnRuleId" :rule-id="ruleId"/>
            <div slot="footer">
                <Button size="small" @click="isShowRuleConfig = false,dataLoad = false">取消</Button>
            </div>
        </Modal>
    </rui-page>
</template>
<script>
    import manualLabelDefine from "../defines/manual-label-define";
    import ruiRules from '_c/rui-auto/rui-rules/index';
    import {queryAtomsByBizType} from "@/projects/basic/api/rule/ruleAtom";
    import {loadRuleDataById} from '_p/basic/api/rule/rule';
    import {deepClone} from "@/libs/utils/ObjectClone";
    import {
        getLabelList,
        addLabel,
        editLabel,
        deleteLabel,
        activateOrExpiredTheRules
    } from "@/projects/afs-core-business/api/afs-case/manual-label/manualLabel";

    export default {
        name: "manual-label",
        components: {
            ruleTree: ruiRules
        },
        props: {},
        data() {
            return {
                labelModalVisible: false,
                modalTitle: "手动标签库管理功能",
                searchFormLabels: [
                    {defineId: "manualLabel", span: 15, fields: ["labelName","labelPhase","labelType"
                        ]},
                    {defineId: "manualLabel", span: 15, fields: [
                            "labelLocation","labelClassification","status"]}
                ],
                labelFormLabels: [
                    {
                        grids: [
                            {
                                defineId: "manualLabel",
                                span: 7,
                                fields: ['labelNo','labelName','labelPhase']
                            },
                            {
                                defineId: "manualLabel",
                                span: 7,
                                fields: ['labelColor','labelType','labelLocation']

                            },
                            {
                                defineId: "manualLabel",
                                span: 7,
                                fields: ['labelClassification','labelSort','status']
                            },
                            {
                                defineId: "manualLabel",
                                span: 7,
                                fields: ['isArchive']

                            }

                        ]

                    }
                ],
                manualLabelColumns: [
                    'opt', 'ruleId','labelNo', 'labelName','labelPhase','labelColor','labelType','labelLocation',
                    'labelClassification','labelSort','status','label','isArchive'
                ],

                dataLoad:false,
                isShow: true,
                isShowRule: false,//规则输出信息页面展示
                ruleTitle: null,//规则输出信息页面主题
                isShowRuleConfig: false,//规则配置页面展示
                ruleConfigTitle: null,//规则配置页面主题
                modalType: 0,
                ruleAtoms: [],
                ruleTree: [],
                ruleName: '标签规则配置',
                ruleGroup: 'riskControlAccess',
                fitScope: 'businessSegment',
                businessScenario: 'businessScenario',
                ruleId : null,
                ruleStatus : '0',
                ruleData:[],
                row:{},
                selectList: [],
                ruleReadOnly:false
            };
        },
        computed: {
            defines() {
                return [
                    {
                        id: "manualLabel",
                        fields: manualLabelDefine
                    }
                ];
            }
        },
        methods: {
            queryData(queryData) {
                queryData.condition.label='automatic';
                getLabelList(queryData).then(res => {
                    if (res.code === "0000") {
                        let {records, total} = res.data;
                        this.$refs.labelTable.updateTableData(records, total);
                    }
                });
                queryAtomsByBizType(this.fitScope,"case").then(res => {
                    if (res.code === "0000") {
                        this.ruleAtoms = res.data;
                    }
                });
            },
            refresh() {
                this.$refs['labelTable'].reloadData();
            },

            labelSubmit() {
                this.$refs.labelForm.getForm().validate(valid => {
                    if (valid) {
                        if (this.modalType === 0) {
                            let data = this.$refs.labelForm.getFormData();
                            this.$refs.labelForm.setAllFieldRequired(true)
                            let reg = /^\d+$/;
                            if (!reg.test(data.labelNo)) {
                                this.$Message.warning("请输入数字组合标签编号！");
                                return;
                            }
                            data.label ="automatic";//默认自动
                            addLabel(data)
                                .then(res => {
                                    if (res.code === "0000") {
                                        this.$Message.success("操作成功");
                                        this.refresh();
                                        this.labelModalVisible = false;
                                    }
                                })
                                .catch(() => {
                                    this.refresh();
                                });
                        } else {
                            let data = this.$refs.labelForm.getFormData();
                            let reg = /^\d+$/;
                            if (!reg.test(data.labelNo)) {
                                this.$Message.warning("请输入数字组合标签编号！");
                                return;
                            }
                            // 编辑
                            editLabel(this.$refs.labelForm.getFormData())
                                .then(res => {
                                    if (res.code === "0000") {
                                        this.$Message.success("操作成功");
                                        this.labelModalVisible = false;
                                        this.refresh();
                                    }
                                })
                                .catch(() => {
                                    this.refresh();
                                });
                        }
                    }
                });
            },
            ruleConfig(row) {
                let status=row.status;
                if(status=='takeEffect'){
                    this.ruleReadOnly = true;
                }else{
                    this.ruleReadOnly = false;
                }
                if(row.ruleId!=null){
                    this.ruleId = row.ruleId;
                    loadRuleDataById(this.ruleId,"case").then(res => {
                        if(res.code='0000'){
                            if(res.data.length>0) {
                                this.ruleData=res.data;
                            }
                            this.dataLoad = true;
                        }
                    });
                }else{
                    this.ruleId = null;
                    this.ruleData = [];
                    this.dataLoad = true;
                }
                this.ruleConfigTitle = "编辑规则";
                this.isShowRuleConfig = true;
                this.row = row;
                this.businessScenario = row.id;
            },
            remove(v) {
                this.$Modal.confirm({
                    title: "确认删除",
                    content: "您确认要删除参数: " + v.labelName + " ?",
                    onOk: () => {
                        deleteLabel(v.id).then(res => {
                            if (res.code === "0000") {
                                this.$Message.success("删除成功");
                                this.refresh();
                            }
                        });
                    }
                });
            },
            add() {
                this.$refs.labelForm.resetFrom();
                this.modalType = 0;
                this.modalTitle = "新增";
                this.labelModalVisible = true;
                this.$refs.labelForm.setAllFieldRequired(true);
            },
            edit(row) {
                let status=row.status;
                if(status=='takeEffect'){
                    this.$Message.warning("生效状态下，不可修改数据!");
                    return;
                }
                this.modalType = 1;
                this.modalTitle = "编辑";
                this.$refs.labelForm.updateFormData(row);
                this.$refs.labelForm.setAllFieldRequired(true);
                this.labelModalVisible = true;
            },
            //设置生效
            takeEffect(row){
                // 编辑
                row.status='takeEffect';
                let flag="activate";
                activateOrExpiredTheRules(row,flag)
                    .then(res => {
                        if (res.code === "0000") {
                            this.$Message.success("操作成功");
                            this.labelModalVisible = false;
                            this.refresh();
                        }
                    })
                    .catch(() => {
                        this.refresh();
                    });
            },
            //设置失效
            failure(row){
                // 编辑
                row.status='failure';
                let flag="expired";
                activateOrExpiredTheRules(row,flag)
                    .then(res => {
                        if (res.code === "0000") {
                            this.$Message.success("操作成功");
                            this.labelModalVisible = false;
                            this.refresh();
                        }
                    })
                    .catch(() => {
                        this.refresh();
                    });

            },
            returnRuleId(ruleId){
                this.ruleId = ruleId;
                this.dataLoad = false;
                this.isShowRuleConfig = false;
                // 编辑
                this.row.ruleId = ruleId;
                editLabel(this.row)
                    .then(res => {
                        if (res.code === "0000") {
                            this.refresh();
                        }
                    })
                    .catch(() => {
                        this.refresh();
                    });
            },
        },
        mounted() {
            this.refresh();
        }
    };
</script>
